package com.mikelouis.test.scala.proginscala.chapter28

/**
 * Finally, a Point class with a good equals method and a good hashCode method.
 * Created by yuazhu on 2/26/2015.
 */
object Points3 {

  class Point(val x: Int, val y: Int) {
    override def hashCode = 41 * (41 + x) + y

    override def equals(other: Any) = other match {
      case that: Point => this.x == that.x && this.y == that.y
      case _ => false
    }
  }

  def main(args: Array[String]) {
    val p1, p2 = new Point(1, 2)
    val q = new Point(2, 3)
    println(p1 equals p2)
    println(p1 equals q)

    // return true now
    val coll = scala.collection.mutable.HashSet(p1)
    println(coll contains p2)
  }

}
